package com.haha.mining;

import javazoom.jl.player.Player;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

/**
 * @author shenlinnan
 * @date 2025/8/10 12:24
 */
public class LoopFind implements Runnable {

    private volatile boolean switch1;

    @Override
    public void run() {
        // 截取屏幕
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
            return;
        }
        Rectangle screenRect = new Rectangle(2340, 80, 160, 160);
        BufferedImage screenCapture = robot.createScreenCapture(screenRect);

        // 保存截图到本地
        try {
            saveScreenshot(screenCapture);
        } catch (IOException e) {
            e.printStackTrace();
        }


        int sum = 0;
        while (switch1 /*&& sum < 10*/) {
            // 截取图片
            screenRect = new Rectangle(2340, 80, 160, 160);
            screenCapture = robot.createScreenCapture(screenRect);

            outerForLoop:
            for (int y = 0; y < screenCapture.getHeight(); y++) {
                for (int x = 0; x < screenCapture.getWidth(); x++) {
                    // 获取当前像素点的ARGB颜色值
                    int pixelColor = screenCapture.getRGB(x, y);

                    // 提取颜色通道（ARGB格式）
                    int alpha = (pixelColor >> 24) & 0xFF;
                    int red = (pixelColor >> 16) & 0xFF;
                    int green = (pixelColor >> 8) & 0xFF;
                    int blue = pixelColor & 0xFF;

                    // 打印像素信息（实际应用中可能需要处理大量数据）
//                    System.out.printf("坐标(%d, %d) - ARGB(%d, %d, %d, %d)%n",
//                            x, y, alpha, red, green, blue);
// 238,186,8
                    //227,177,9
                    // 239,186,8
                    if ((red == 228 && green == 178 && blue == 10) ||
                            (red == 226 && green == 176 && blue == 9) ||
                            (red == 238 && green == 186 && blue == 8) ||
                            (red == 227 && green == 177 && blue == 9) ||
                            (red == 239 && green == 186 && blue == 8)) {
//                        System.out.println("找到要找的颜色了....");

                        try {
                            FileInputStream fis = new FileInputStream("J:\\World of Warcraft\\_classic_\\Interface\\AddOns\\BiaoGe\\Media\\sound\\AI\\心愿达成.mp3");
                            Player player = new Player(fis);
                            player.play(); // 阻塞直到播放结束
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        break outerForLoop;
                    }

                }
            }

            sum++;
//            System.out.println("执行了..." + sum + "次");
            robot.delay(1000);
        }
    }

    /**
     * 保存截图到本地磁盘
     *
     * @param image 要保存的图像
     */
    private void saveScreenshot(BufferedImage image) throws IOException {
        // 创建保存目录（如果不存在）
        File dir = new File("screenshots");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // 生成带时间戳的文件名
        String timestamp = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
        String fileName = "screenshot_" + timestamp + ".png";
        File outputFile = new File(dir, fileName);

        // 将图像保存为PNG格式
        ImageIO.write(image, "png", outputFile);

        System.out.println("截图已保存至: " + outputFile.getAbsolutePath());
    }

    public boolean isSwitch1() {
        return switch1;
    }

    public void setSwitch1(boolean switch1) {
        this.switch1 = switch1;
    }
}
